package com.ycfmedical.bed.utils;

import com.github.mikephil.charting.data.Entry;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.droidlover.xdroidmvp.log.XLog;

/**
 * 计算睡眠质量工具类
 * Created by Administrator on 2017-04-30.
 */

public class SleepCount {
    /**
     * 计算睡眠数值
     */
    public static ArrayList<Entry> askSleep(List<HashMap<String, String>> list) {
        ArrayList<Entry> entrys = new ArrayList<>();
        int num = 1;
        int data = 0;
        for (int i = 0; i < list.size(); i++) {
            if (ConvertOrderCheckoutUtil.StrC16T10(list.get(i).get("心率")) == 0) {
                continue;
            }
            num++;
            if (list.get(i).get("体动").equals("4F")) {
                data += 40;
            }
            data += ConvertOrderCheckoutUtil.StrC16T10(list.get(i).get("心率").replace(" ", ""));
            data += ConvertOrderCheckoutUtil.StrC16T10(list.get(i).get("呼吸").replace(" ", ""));
            if (num == 10 || i == list.size()) {
                entrys.add(new Entry(entrys.size(), data / num));
                data = 0;
                num = 1;
            }
        }
        return entrys;
    }

    /**
     * 计算睡眠时长
     */
    public static String countSleep(List<HashMap<String, String>> list, ArrayList<Entry> entrys) {
        return CountTime.count(countStart(list, entrys), countRise(list, entrys));
    }

    public static String countStart(List<HashMap<String, String>> list, ArrayList<Entry> entrys) {

        String StartTime = "";
        for (int sleep = 0; sleep < entrys.size(); sleep++) {
            if (entrys.get(0).getY() - 5 > entrys.get(sleep).getY()) {
                StartTime = list.get(sleep * 10).get("时间");
                break;
            }
        }
        return StartTime;
    }

    public static String countRise(List<HashMap<String, String>> list, ArrayList<Entry> entrys) {
        String entTime = "";
        for (int rise = 0; rise < entrys.size(); rise++) {
            if (entrys.get(entrys.size() - 1).getY() - 5 > entrys.get(entrys.size() - (rise + 1)).getY()) {
                entTime = list.get((list.size() - rise * 10)).get("时间");
                break;
            }
        }
        return entTime;
    }

    /**
     * 计算平均呼吸
     */
    public static String countAvBreathe(List<HashMap<String, String>> list) {
        int avBreathe = 0;
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                avBreathe += ConvertOrderCheckoutUtil.StrC16T10(list.get(i).get("呼吸").replace(" ", ""));
            }
            avBreathe = avBreathe / list.size();
        }
        return avBreathe + "";
    }

    /**
     * 计算平均心率
     */
    public static String countAvHeart(List<HashMap<String, String>> list) {
        int avHeart = 0;
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                avHeart += ConvertOrderCheckoutUtil.StrC16T10(list.get(i).get("心率").replace(" ", ""));
            }
            avHeart = avHeart / list.size();
        }
        return avHeart + "";
    }

    /**
     * 计算睡眠状态
     */
    public static Integer[] sleepStatus(ArrayList<Entry> lists) {
        if (lists == null && lists.size() < 10) {
            return null;
        }
        Integer[] status = new Integer[4];
        int sleepEmbed = 0;       //深睡
        int sleepCommonly = 0;    //中睡
        int sleepBadly = 0;       //浅睡
        int sleepSober = 0;       //清醒
        float num = 0;
        for (int i = 0; i < lists.size(); i++) {
            num = lists.get(i).getY();
            if (num > 90) {
                sleepEmbed += 1;

            } else if (num > 80) {
                sleepCommonly += 1;

            } else if (num > 70) {
                sleepBadly += 1;

            } else if (num > 60) {
                sleepSober += 1;
            }
            num = 0;
        }
        if(lists != null && lists.size() != 0){
            status[0] = (sleepEmbed * 100) / lists.size();
            status[1] = (sleepCommonly * 100) / lists.size();
            status[2] = (sleepBadly * 100) / lists.size();
            status[3] = (sleepSober * 100) / lists.size();
        }

        return status;
    }

    /**
     * 计算X轴标签方法
     */
    public static String[] XContText(List<HashMap<String, String>> list) {
        String[] XText = new String[(list.size() / 10) + 10];
        int num = 0;
        int XCont = 0;
        for (int i = 0; i < list.size(); i++) {
            num++;
            if (num == 9) {

                XText[XCont] = list.get(i).get("时间").substring(10, 16);
                XCont++;
                num = 0;
            }
        }
        return XText;
    }

}
